//
//  Status.swift
//  MyWeibo
//
//  Created by tanchao on 15/8/3.
//  Copyright © 2015年 chaoren. All rights reserved.
//

import UIKit
import SDWebImage
/// 微博模型
class Status: NSObject {

    // 微博创建时间
    var created_at : String?
    //	微博ID
    var id : Int = 0
    // 微博信息内容
    var text: String?
    // 微博来源
    var source: String?
    // 配图数组
    var pic_urls : [[String : AnyObject]]?{
        didSet{
            if pic_urls?.count == 0{
               return
            }
            storedpicURLS = [NSURL]()
            for dic in pic_urls!{
                if let urlstr = dic["thumbnail_pic"] as? String{
                    storedpicURLS?.append(NSURL(string: urlstr)!)
                }
            }
        }
    }
    private var storedpicURLS : [NSURL]?
    var picURLS : [NSURL]?{
        return retweeted_status == nil ? storedpicURLS : retweeted_status?.storedpicURLS
    }
    //微博博主
    var user:User?
    /// 转发微博
    var retweeted_status:Status?
   
    var rowHeight:CGFloat?
    /// 转发数
    var reposts_count:Int = 0
    /// 评论数
    var comments_count:Int = 0
    /// 表态数
    var attitudes_count:Int = 0
    class func loadStatus(since_id : Int?,max_id : Int?, finished:(datalist:[ Status]?,error:NSError?) -> ()){
        WeiboNetWorkingTools.sharedNetWorkingTools.loadStatus(since_id, max_id: max_id){ (result, error) -> () in

            if error != nil{
                finished(datalist: nil, error: error)
                return
            }
            if let array = result?["statuses"] as? [[String: AnyObject]]{
                var list = [Status]()
                for dict in array{
                    list.append(Status(dict: dict))
                }
                cacheWebImage(list, finished: finished)
//                finished(datalist: list, error: nil)
            }else{
                finished(datalist: nil, error: nil)
            }
        }
    }
    class private func cacheWebImage(list:[Status],finished:(datalist:[ Status]?,error:NSError?) -> ()) {
         var dataLength = 0
        let group = dispatch_group_create()
        for status in list{
            guard let urls = status.picURLS else{
                continue
            }
            for url in urls{
                dispatch_group_enter(group)
                SDWebImageManager.sharedManager().downloadImageWithURL(url, options: SDWebImageOptions(rawValue: 0), progress: nil, completed: { (image, _, _, _, _)  in
              let data =   UIImagePNGRepresentation(image)!
                    dataLength += data.length
                    dispatch_group_leave(group)
                })
            }
        }
        dispatch_group_notify(group, dispatch_get_main_queue()) {
            
            finished(datalist: list, error: nil)
        }
    }
    
    init(dict:[String : AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user" {
            if let dict = value as? [String:AnyObject]{
                user = User(dict: dict)
                return
            }
            
        }
        if key == "retweeted_status" {
            if let dict = value as? [String:AnyObject]{
                retweeted_status = Status(dict: dict)
                return
            }
            
        }
        super.setValue(value, forKey: key)
    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    /// 对象描述信息
    override var description: String {
        let properties = ["created_at","id","text","source","pic_urls"]
        return "\(dictionaryWithValuesForKeys(properties))"
    }
}

